home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / gfx / lise2.1 / lise / src / tcal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-31  |  2.9 KB  |  99 lines

  1. /* 
  2.    Time calibration:
  3.    counts the peaks in the spectrum and divides this number (minus one) by
  4.    the number of channels between the first and the last peak.
  5.    This value is then optionally multiplied by the time
  6.    (in whatever units you like) between two peaks to give the exact time
  7.    between two channels.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <spec.h>
  12. #define TMPFILE "tica.all"
  13.  
  14. float *spc, *err, *tim;
  15. int peak_min=10,
  16.     diff_min=5;
  17.  
  18. help()
  19. {
  20.   printf("time calibration:\n");
  21.   printf("tcal file [options]\n");
  22.   printf("prints time difference between channels\n");
  23.   printf("options:\n");
  24.   printf("  -t n.m   set time difference between two peaks\n");
  25.   printf("  -y n     set minimum height for peaks\n");
  26.   printf("  -mean    set minimum height for peaks to arithmetic mean of spectrum\n");
  27.   printf("  -diff    set minimum difference between peaks\n");
  28.   printf("  -peak    print peak positions\n");
  29.   printf("  -quiet   don't print anything (output goes to %s)\n",TMPFILE);
  30.   exit(0);
  31. }
  32.  
  33. main(argc,argv)
  34. int argc;
  35. char *argv[];
  36. {
  37. int first,last,sptr,n,m,i,max,npeaks;
  38. int maxdiff, mindiff;
  39. char z[80];
  40. float x,y,timd=1.0,tica_err;
  41. FILE *fp;
  42.  
  43.    checkopt(argc,argv,"-dummy",z);
  44.    spc = (float *) calloc(_MAXSPCLEN+2,sizeof(float));
  45.    err = (float *) calloc(_MAXSPCLEN+2,sizeof(float));
  46.    tim = (float *) calloc(_MAXSPCLEN+2,sizeof(float));
  47.  
  48.    first=0; last=0; npeaks=0;
  49.    if(checkopt(argc,argv,"-t",z)) timd=atosf(z);
  50.    if(checkopt(argc,argv,"-y",z)) peak_min=atoi(z);
  51.    if(checkopt(argc,argv,"-diff",z)) diff_min=atoi(z);
  52.    max=readspec(argv[1],spc,err,tim,z);
  53.    if(checkopt(argc,argv,"-mean",z)) {
  54.       for(n=0;n<max;n++) peak_min = peak_min + spc[n];
  55.       peak_min = peak_min / max;
  56.       peak_min = 2 * peak_min;
  57.    }
  58.                                          /* now searching for peaks */
  59.    sptr=0; maxdiff = 0; mindiff = 100000;
  60.    while(sptr<max) {
  61.    sptr=sptr+1;
  62.       while(spc[sptr] <= spc[sptr+1]) {             /* find next maximum */
  63.          sptr=sptr+1;
  64.          if(sptr>max) break;
  65.       }
  66.       n=spc[sptr];
  67.       if(n<peak_min) continue;
  68.       i=sptr-last;
  69.       if(i<diff_min) continue;
  70.       last=sptr;
  71.       if(first==0) first=sptr;
  72.       if(npeaks > 0) {
  73.          if(i > maxdiff) maxdiff = i;
  74.          if(i < mindiff) mindiff = i;
  75.       }
  76.       npeaks=npeaks+1;
  77.       if(checkopt(argc,argv,"-peak",z)) {
  78.          printf("peak at %d      difference %d      height %d\n",sptr,i,n);
  79.       }
  80.       while(spc[sptr] > spc[sptr+1]) {             /* find next minimum */
  81.          sptr=sptr+1;
  82.          if(sptr>max) break;
  83.       }
  84.    }
  85.    y = ((float)(npeaks - 1)) / ((float)(last - first));
  86.    y = y * timd;
  87.    tica_err = (((float)(maxdiff - mindiff))*1.4142)/((float)(last - first));
  88.    tica_err = y * tica_err;
  89.    if(!checkopt(argc,argv,"-quiet",z)) {
  90.      printf("%d peaks found\n",npeaks);
  91.      printf("time between channels is %5.5f +- %5.5f\n",y,tica_err);
  92.    }
  93.    fp=fopen(TMPFILE,"w");
  94.    fprintf(fp,"%E\n",y);
  95.    fclose(fp);
  96.    return(0);
  97. }
  98.  
  99.